package com.ljkj.cordial.chat.adapter;

import android.annotation.SuppressLint;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.lanjiu.lib.imclient.ims.IMSContacts;
import com.ljkj.cordial.chat.R;
import com.ljkj.cordial.chat.dao.notify.BusinessLeaveTable;
import com.ljkj.cordial.chat.widget.PuzzleImageView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 通知
 */
public class NavNotifyAdapter extends RecyclerView.Adapter<NavNotifyAdapter.ViewHolder> {

    private List<BusinessLeaveTable> mList;
    private OnNotifyItemClickListener mOnNotifyItemClickListener;
    private OnNotifyItemLongClickListener mOnNotifyItemLongClickListener;

    public NavNotifyAdapter() {
        mList = new ArrayList<>();
    }

    public void setList(List<BusinessLeaveTable> list) {
        mList.clear();
        mList.addAll(list);
        notifyDataSetChanged();
//        notifyItemRangeChanged(0, list.size());
    }

    public void setOnNotifyItemClickListener(OnNotifyItemClickListener mOnNotifyItemClickListener) {
        this.mOnNotifyItemClickListener = mOnNotifyItemClickListener;
    }

    public void setOnNotifyItemLongClickListener(OnNotifyItemLongClickListener mOnNotifyItemLongClickListener) {
        this.mOnNotifyItemLongClickListener = mOnNotifyItemLongClickListener;
    }

    private BusinessLeaveTable getItem(int position) {
        return mList.get(position);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new ViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_nav_notify, viewGroup, false));
    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int i) {
        BusinessLeaveTable msg = getItem(i);
        if (!TextUtils.isEmpty(msg.getTargetUserName())) {
            holder.tvTitle.setText(msg.getTargetUserName());
        } else if (!TextUtils.isEmpty(msg.getTargetUserId())) {
            holder.tvTitle.setText("ID:" + msg.getTargetUserId());
        } else if (!TextUtils.isEmpty(msg.getTargetGroupName())) {
            holder.tvTitle.setText(msg.getTargetGroupName());
        } else if (!TextUtils.isEmpty(msg.getTargetGroupId())) {
            holder.tvTitle.setText("ID:" + msg.getTargetGroupId());
        }
        holder.tvNum.setVisibility(1 == msg.getIsRead() ? View.INVISIBLE : View.VISIBLE);
        if (IMSContacts.UserType.REGISTERED_USER.equals(msg.getTargetUserType())) {
            holder.tvVisitorHint.setVisibility(View.GONE);
            holder.ivIsVisitor.setVisibility(View.GONE);
        } else {
            holder.tvVisitorHint.setVisibility(View.VISIBLE);
            holder.ivIsVisitor.setVisibility(View.VISIBLE);
        }
        holder.tvContent.setText(msg.getTargetLeave());
        if (TextUtils.isEmpty(msg.getAvatarUrl())) {
            holder.piv.setImageResource(R.mipmap.pic_user_avatar_default_36dp);
        } else {
            if (msg.getAvatarUrl().contains(",")) {
                holder.piv.setImageBackGround(msg.getAvatarUrl(), msg.getTargetUserId());
            } else {
                holder.piv.setFriendUrl(msg.getAvatarUrl(), msg.getTargetUserId());
            }
        }
        holder.itemView.setOnClickListener(v -> {
            if (mOnNotifyItemClickListener != null)
                mOnNotifyItemClickListener.onNotifyItemClick(v, holder.getAdapterPosition(), msg);
        });
        holder.itemView.setOnLongClickListener(v -> {
            if (mOnNotifyItemLongClickListener != null)
                mOnNotifyItemLongClickListener.onNotifyItemLongClick(v, holder.getAdapterPosition(), msg);
            return true;
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.piv)
        PuzzleImageView piv;
        @BindView(R.id.tv_title)
        TextView tvTitle;
        @BindView(R.id.tv_content)
        TextView tvContent;
        @BindView(R.id.tv_num)
        TextView tvNum;
        @BindView(R.id.iv_is_visitor)
        ImageView ivIsVisitor;
        @BindView(R.id.tv_visitor_hint)
        TextView tvVisitorHint;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnNotifyItemClickListener {
        void onNotifyItemClick(View view, int position, BusinessLeaveTable bean);
    }

    public interface OnNotifyItemLongClickListener {
        void onNotifyItemLongClick(View view, int position, BusinessLeaveTable bean);
    }

    public void remove(int position) {
        if (position >= 0 && position < mList.size()) {
            mList.remove(position);
            notifyItemRemoved(position);
        }
    }
}
